package com.hdrx.fundhelper.utils;

import org.springframework.expression.ParseException;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: houdx
 * @description:
 * @date: 2023/7/25 19:17
 */
public class DateUtil {

    /**
     * string类型日期比较
     * @param date1
     * @param date2
     */
    public static void compareDate(String date1, String date2){
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d1 = dateFormat.parse(date1);
            Date d2 = dateFormat.parse(date2);
            if(d1.equals(d2)){
                System.out.println(date1+"="+date2);
            }else if(d1.before(d2)){
                System.out.println(date1+"在"+date2+"之前");
            }else if(d1.after(d2)){
                System.out.println(date1+"在"+date2+"之后");
            }
        } catch (ParseException | java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("方法——compareDate（{}，{}）异常"+date1+","+date2);
        }
    }
    /**
     * string类型日期比较
     * @param date1
     * @param date2
     */
    public static void compareDate1(String date1,String date2){
        if (date1.compareTo(date2)>0){
            System.out.println(date1+"在"+date2+"之后");
        }else if (date1.compareTo(date2) ==0){
            System.out.println(date1+"="+date2);
        }else if (date1.compareTo(date2)<0){
            System.out.println(date1+"在"+date2+"之前");
        }


    }

    public static void main(String[] args) {
        String d1 = "2023-01-05";
        String d2 = "2023-01-06";
        compareDate1(d1,d2);
    }
}
